<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 论坛接口
 */
class Forum extends Api
{
    protected $noNeedLogin = ['page','detail'];
    protected $noNeedRight = ['*'];

    /**
     * 发布
     *
     */
    public function add()
    {
        $data = [
            'user_id'=>$this->auth->id,
            'forum_name'=>input('forum_name',''),
            'forum_content'=>input('forum_content',''),
            'images'=>input('images',''),
        ];

        if (empty($data['forum_name'])) {
            $this->error('请输入标题');
        }
        if (empty($data['forum_content'])) {
            $this->error('请输入内容');
        }

        $forum_model = new \app\api\model\forum\Forum();

        $res = $forum_model->save($data);

        $this->success('请求成功',$res);
    }

    //我的发布
    public function my()
    {
        $where = ['f.user_id'=>$this->auth->id];
        $orderBy = 'f.createtime desc';
        $list = \app\api\model\forum\Forum::alias('f')
            ->join('users u', 'u.id=f.user_id', 'LEFT')
            ->field('f.*,u.nickname,u.avatar_url')
            ->where($where)
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);

        if ($this->auth->id){
            foreach ($list as $k=>$v){
                $collect = \app\api\model\forum\ForumCollect::get(['forum_id'=>$v['forum_id'],'user_id'=>$this->auth->id]);
                $list[$k]['is_collect'] = $collect?1:0;
            }
        }

        $this->success('请求成功',$list);
    }

    //删除
    public function delete()
    {
        $forum_id = input('forum_id',0);
        if (empty($forum_id)) {
            $this->error('缺少参数论坛id');
        }

        $info = \app\api\model\forum\Forum::where(['forum_id'=>$forum_id,'user_id'=>$this->auth->id])->find();
        if (!$info){
            $this->error('不是当前用户帖子');
        }

        $res = $info->delete();

        if ($res){
            $this->success('删除成功');
        } else {
            $this->success('删除失败');
        }
    }

    //论坛大厅
    public function page()
    {
        //$order = input('order','hot');
        $where = [];
        /*if ($order=='hot'){
            $orderBy = 'f.comment_num desc';
        } else {
            $orderBy = 'f.createtime desc';
        }*/
        $orderBy = 'f.createtime desc';
        $list = \app\api\model\forum\Forum::alias('f')
            ->join('users u', 'u.id=f.user_id', 'LEFT')
            ->field('f.*,u.nickname,u.avatar_url')
            ->where($where)
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);
        if ($this->auth->id){
            foreach ($list as $k=>$v){
                $collect = \app\api\model\forum\ForumCollect::get(['forum_id'=>$v['forum_id'],'user_id'=>$this->auth->id]);
                $list[$k]['is_collect'] = $collect?1:0;
            }
        }

        $this->success('请求成功',$list);
    }

    public function addrepost()
    {
        $forum_id = input('forum_id',0);
        \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setInc('repost_num',1);
        $this->success('');
    }


    //帖子详情
    public function detail()
    {
        $forum_id = input('forum_id',0);

        $detail = \app\api\model\forum\Forum::alias('f')
            ->join('user u', 'u.id=f.user_id', 'LEFT')
            ->where(['f.forum_id'=>$forum_id])
            ->field('f.*,u.nickname,u.avatar')
            ->find();

        $detail['comment_num'] = \app\api\model\forum\ForumComment::where(['forum_id'=>$forum_id])->count();
        $this->success('请求成功',$detail);
    }


}
